var express = require('express');
var router = express.Router();
const userModel = require('../../models/userModel');
const roleModel = require('../../models/roleModel');
const staffModel = require('../../models/staffModel');
const subaccModel = require('../../models/subaccountModel');
const accessModel = require('../../models/accessModel');
const {domain,secret} = require('../../config/config');
const jwt = require('jsonwebtoken');
let checkTokenMiddleware = require('../../middlewares/checktoken');
const checkLogin = require('../../middlewares/checklogin');

router.get('/login',(req,res)=>{
    res.render('adminLogin');
});


router.post('/login', function(req, res, next) {
   

 
    
    let {admin_name,password} =req.body;
    
    userModel.findOne({user:admin_name,password:password})
    .then(data => {
        console.log(data);
        if(data){
        
            console.log(data);
            req.session.user =data.user;
            res.render('success',{msg:'登录成功',url:'/admin/access'});
 
        }else{
     
            res.render('fail',{msg:'登录失败',url:'/admin/login'});
        }

    })
    .catch(err => {
        console.log(err)
        res.render('fail',{msg:'账号密码错误',url:'/admin/login'});
    });
    
    
});
router.get('/access',checkLogin, function(req, res, next) {
    let user = req.session.user

    userModel.findOne({user:user})
    .then(data => {
        if(data){
    
            res.render('access',{
                user:user,
                password:data.password
            });
 
        }else{
            res.render('access',{
                user:null,
                password:null
            });
        }

    })
    .catch(err => {
        console.log(err)
        res.render('access',{
            user:null,
            password:null
        });

    });

});

router.get('/register',(req,res)=>{
    res.render('register');
});

router.post('/register',(req,res)=>{
    //查询数据库
    let {user,password} =req.body;
    userModel.findOne({user:user})
    .then(data => {
        console.log(data);
        if(!data){
            roleModel.findOne({name:"user"}).then(data=>{
                if(!data){
                    res.render('fail',{msg:'注册失败,暂无该用户权限,请稍后再试',url:'/admin/register'});
                    return;
                }else{
                    userModel.create({user:user,password:password,permissions:data.id,desc:"普通用户权限",state:0}).then(data => {
                        if(!data){
                            res.render('fail',{msg:'注册失败,请稍后再试',url:'/admin/register'});
                            return;
                        }
                        res.render('success',{msg:'注册成功',url:'/admin/login'});
                    
                    })
                    .catch(err => {
                        res.render('fail',{msg:'注册失败,请稍后再试',url:'/admin/register'});
                     
                    });
                }
            })
            
        }else{
            res.render('fail',{msg:'注册失败,用户已存在',url:'/register'});
        }
        
       
    })
    .catch(err => {
        res.render('fail',{msg:'注册失败',url:'/register'});
    });
    
});

module.exports = router;
